﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Remoting;
using System.Text;
using System.Threading;
using System.Timers;
using System.Windows.Forms;
using lx;
using lx.UI;
using lxhy.Properties;
using Microsoft.Win32;

namespace lxhy
{
    public partial class HYForm : Form
    {
        private PageData para = new PageData();
        private int x = 0;
        private int y = 0;
        private int time = 3600;
        private string jgsjStr = "60";

        public HYForm()
        {
            InitializeComponent();
        }
        // 定时器触发的事件处理程序
        private void OnTimedEvent(Object source, EventArgs e)
        {
            System.Drawing.Point mp = System.Windows.Forms.Control.MousePosition;
            int mousex = mp.X;  //鼠标当前X坐标
            int mousey = mp.Y;  //鼠标当前Y坐标
            if (x != mousex || y != mousey)
            {
                //Console.WriteLine("监听到鼠标移动!重置时间!");
                exeque();
            }
            x = mousex; y = mousey;
            sysj.Text = string.Format("关机倒计时:{0}秒", time--);
            tongzhilan.Text ="倒计时: " +time; //显示下次执行时间
            if (time>=179)
            {
                hide();
            }else{
                show(null,null);
            }
            if(time == 0)
            {
                exit(null,null);
                SystemUtil.PowerOff();//关机
            }


        }

        ContextMenuStrip myMenu;
        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Interval = 1000;
            timer1.Start();
            timer1.Tick += OnTimedEvent;

            string gjsj = IOUtil.GetAppConfig("gjsj");
            if(gjsj != null) {
                jiangeshijian.Text = gjsj;
            }
            string kjqd = IOUtil.GetAppConfig("kjqd");
            if (kjqd == null)
            {
                CB.Checked = true;
                IOUtil.UpdateAppConfig("kjqd","1");
            }else if(kjqd == "1")
            {
                CB.Checked = true;
            }


                List<PageData> list = new List<PageData>();
            //MyMenu.addColToList(list, "设置", new System.EventHandler(show), null, null);
            //MyMenu.addColToList(list, "退出", new System.EventHandler(exit), null, null);
            MyMenu m = new MyMenu();
            m.addMenu(list);
            myMenu = m.contextMenuStrip1;
            this.tongzhilan.ContextMenuStrip = myMenu;

            //this.tongzhilan.Visible = true;//在通知区显示Form的Icon
            this.tongzhilan.Icon = this.Icon;
            //this.ShowInTaskbar = false;///使窗体不显示在任务栏
            this.MaximizeBox = false; //不显示最大化按钮

            this.pictureBox1.SendToBack();//将背景图片放到最下面
            this.panel1.BackColor = Color.Transparent;//将Panel设为透明
            this.panel1.Parent = this.pictureBox1;//将panel父控件设为背景图片控件
            this.panel1.BringToFront();//将panel放在前面
            hide();
            exeque();
            CB_CheckedChanged(null,null);//开机启动
        }
        /// <summary>
        /// 关闭窗口
        /// </summary>
        private void HYForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)//当用户点击窗体右上角X按钮或(Alt + F4)时 发生          
            {
                e.Cancel = true;
                hide();
            }
        }      
        /// <summary>
        /// 显示窗口
        /// </summary>
        void show(object sender, EventArgs e)
        {
            
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;

            
        }
        public void hide()
        {
            this.ShowInTaskbar = false;//使窗体不显示在任务栏
            this.Visible = false;
            this.Hide();
        }
        void exit(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void exeque()
        {
            if (LXUtil.IsInt(jiangeshijian.Text))
            {
                if(jgsjStr != jiangeshijian.Text)
                {
                    IOUtil.UpdateAppConfig("gjsj", jiangeshijian.Text);//修改时间
                }
                int jgsj = int.Parse(jiangeshijian.Text);
                time = jgsj * 60;
                //DateTime dt = DateTime.Now;//获取当前时间
                //dt = dt.AddMinutes(jgsj);//将当前时间加上间隔时间得到下次执行时间
                //tongzhilan.Text ="关机时间: " + dt.ToLongTimeString().ToString(); //显示下次执行时间
            }
        }

        private void showDP()
        {
            PageData p = new PageData();
            p.Add("Interval", (int)para["dpsj"]);
            Form dcForm = new dcForm(p);//设置挡屏时间
            dcForm.Show();
        }


        private void HYForm_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                hide();
            }
        }



        private void CB_CheckedChanged(object sender, EventArgs e)
        {
            IOUtil.UpdateAppConfig("kjqd", CB.Checked ?"1":"0");
            LXUtil.KJQD(CB.Checked,"LXHY_KJQD");//开机启动
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

    }
}
